package com.ruoyi.supervise.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.BaseBulidUnitInfo;
import com.ruoyi.supervise.domain.BaseCorpCertDetailInfo;
import com.ruoyi.supervise.domain.BaseCorpCertInfo;
import com.ruoyi.supervise.domain.BasicScoreContent;
import com.ruoyi.supervise.domain.CreditPerformanceContent;
import com.ruoyi.supervise.domain.Enterprise;
import com.ruoyi.supervise.domain.EnterpriseTable;
import com.ruoyi.supervise.domain.IGMCompanyCreditRank;
import com.ruoyi.supervise.domain.IGMCompanyPoorInfo;
import com.ruoyi.supervise.domain.IGMCompanyStageRank;
import com.ruoyi.supervise.domain.IGMCompanyWellInfo;
import com.ruoyi.supervise.domain.IntegrityPerformance;
import com.ruoyi.supervise.domain.ModifyRecs;
import com.ruoyi.supervise.mapper.EnterpriseMapper;
import com.ruoyi.supervise.service.IEnterpriseService;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

/**
 * @ClassName: EnterpriseServiceImpl
 * @Description: 企业档案库impl
 * @Author wtj
 * @DateTime 2020年3月24日 上午11:53:41
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class EnterpriseServiceImpl implements IEnterpriseService 
{
    @Autowired
    private EnterpriseMapper enterpriseMapper;

    @Value("${entSupervise.documentScoreContent}")
    private String documentScoreContent;
    
    @Value("${entSupervise.twoCodeScoreContent}")
    private String twoCodeScoreContent;
    
    @Value("${entSupervise.priceScoreContent}")
    private String priceScoreContent;
    
    @Value("${entSupervise.noComplaintScoreContent}")
    private String noComplaintScoreContent;
    
    @Value("${entSupervise.inspectionScoreContent}")
    private String inspectionScoreContent;
    
	@Override
	public List<EnterpriseTable> selectEnterpriseTableList(EnterpriseTable enterpriseTable) {
		if(StringUtils.isEmpty(enterpriseTable.getBeginPubDate()) && !StringUtils.isEmpty(enterpriseTable.getEndPubDate())) {
			enterpriseTable.setBeginPubDate("1970-01-01");
		}else if(!StringUtils.isEmpty(enterpriseTable.getBeginPubDate()) && StringUtils.isEmpty(enterpriseTable.getEndPubDate())) {
			enterpriseTable.setEndPubDate(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		return enterpriseMapper.selectEnterpriseTableList(enterpriseTable);
	}

	@Override
	public List<EnterpriseTable> selectQualificationsByCompanyId(String guid) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectQualificationsByCompanyId(guid);
	}

	@Override
	public Enterprise selectEnterpriseInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectEnterpriseInfoByGuid(guid);
	}

	@Override
	public List<BaseCorpCertInfo> selectBaseCorpCertInfoByCompanyGuid(String companyGuid) {
		List<BaseCorpCertInfo> bcciList = enterpriseMapper.selectBaseCorpCertInfoByCompanyGuid(companyGuid);
		if(!StringUtils.isEmpty(bcciList)) {
			for(BaseCorpCertInfo bcci:bcciList) {
				String[] subStr=StrUtil.split(bcci.getCertImage(), "|");
				if(subStr.length == 4) {
					bcci.setCertImage(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
				}else {
					bcci.setCertImage(null);
				}
				if(!StringUtils.isEmpty(bcci.getModifyRecs())) {
					ModifyRecs mr = new ModifyRecs();
					mr.setGuid("Guid");
					mr.setType("Type");
					mr.setContent("Content");
					mr.setCreateDate("CreateDate");
					mr.setPersonCertificateBillGuid("PersonCertificateBillGuid");
					String nodePath = "/PsDataPacket/FDataList/ModifyInfo";
					List<ModifyRecs> mrList = XmlReadUtil.getModifyRecsXml(bcci.getModifyRecs(), nodePath, mr);
					bcci.setModifyRecsEntity(mrList);
				}
				List<BaseCorpCertDetailInfo> bccdi = enterpriseMapper.selectBaseCorpCertDetailInfoByCorpCertGuid(bcci.getGuid());
				bcci.setCorpCertDetailInfo(bccdi);
			}
		}
		return bcciList;
	}

	@Override
	public List<IntegrityPerformance> selectIntegrityPerformanceListByCreditCompanyInfoGuid(
			IntegrityPerformance ip) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectIntegrityPerformanceListByCreditCompanyInfoGuid(ip);
	}

	@Override
	public IntegrityPerformance selectIntegrityPerformanceByGuid(String guid) {
		IntegrityPerformance ip = enterpriseMapper.selectIntegrityPerformanceByGuid(guid);
		if(ip != null && !StringUtils.isEmpty(ip.getCreditPerformanceContentList())) {
			CreditPerformanceContent cpc = new CreditPerformanceContent();
			cpc.setGuid("Guid");
			cpc.setCheckContent("CheckContent");
			cpc.setCheckTime("CheckTime");
			cpc.setWorkDone("WorkDone");
			cpc.setPrincipal("Principal");
			cpc.setPrincipalPhone("PrincipalPhone");
			cpc.setCheckTimeEndDate("CheckTimeEndDate");
			cpc.setCheckProjectType("CheckProjectType");
			String nodePath = "/PsDataPacket/FDataList/CreditPerformanceContent";
			List<CreditPerformanceContent> cpcList = XmlReadUtil.getCreditPerformanceContentXml(ip.getCreditPerformanceContentList(), nodePath, cpc);
			ip.setCpcList(cpcList);
		}
		return ip;
	}

	@Override
	public List<IGMCompanyWellInfo> selectIGMCompanyWellInfoListByCompanyGuid(IGMCompanyWellInfo igmcwi) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectIGMCompanyWellInfoListByCompanyGuid(igmcwi);
	}

	@Override
	public IGMCompanyWellInfo selectIGMCompanyWellInfoByGuid(String guid) {
		IGMCompanyWellInfo igmcwi = enterpriseMapper.selectIGMCompanyWellInfoByGuid(guid);
		if(igmcwi!=null) {
			String[] subStr=StrUtil.split(igmcwi.getMaterials(), "|");
			if(subStr.length >= 2) {
				igmcwi.setMaterials(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
			}else {
				igmcwi.setMaterials(null);
			}
		}
		return igmcwi;
	}

	@Override
	public List<IGMCompanyPoorInfo> selectIGMCompanyPoorInfoListByCompanyGuid(IGMCompanyPoorInfo igmcpi) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectIGMCompanyPoorInfoListByCompanyGuid(igmcpi);
	}

	@Override
	public IGMCompanyPoorInfo selectIGMCompanyPoorInfoByGuid(String guid) {
		IGMCompanyPoorInfo igmcpi = enterpriseMapper.selectIGMCompanyPoorInfoByGuid(guid);
		if(igmcpi!=null) {
			String[] subStr=StrUtil.split(igmcpi.getMaterials(), "|");
			if(subStr.length >= 2) {
				igmcpi.setMaterials(Global.getConfig("ruoyi.bigPlatform")+subStr[1]);
			}else {
				igmcpi.setMaterials(null);
			}
		}
		return igmcpi;
	}

	@Override
	public List<IGMCompanyCreditRank> selectIGMCompanyCreditRankBycompanyGuid(String companyGuid) {
		List<IGMCompanyCreditRank> igmccrList = enterpriseMapper.selectIGMCompanyCreditRankBycompanyGuid(companyGuid);
		//获取基础信息构成集合，企业诚信档案阶段评价，处理分数，处理tab名字
		if(!StringUtils.isEmpty(igmccrList)) {
			for(IGMCompanyCreditRank ccr:igmccrList) {
				//获取企业良好行为信息列表
				IGMCompanyWellInfo igmcwi = new IGMCompanyWellInfo();
				igmcwi.setCompanyGuid(companyGuid);
				igmcwi.setQualificationType(ccr.getQualificationType());
				List<IGMCompanyWellInfo> igmcwiList = enterpriseMapper.selectIGMCompanyWellInfoListByCompanyGuid(igmcwi);
				List<BasicScoreContent> companyWellInfoList = new ArrayList<BasicScoreContent>();
				if(!StringUtils.isEmpty(igmcwiList)) {
					for(IGMCompanyWellInfo cwi:igmcwiList) {
						BasicScoreContent companyWellInfo = new BasicScoreContent();
						companyWellInfo.setDate(cwi.getAwardDate());
						companyWellInfo.setContent(cwi.getBehaviorDescription());
						companyWellInfo.setScore(Double.valueOf(cwi.getScore()));
						companyWellInfoList.add(companyWellInfo);
					}
				}
				//获取企业不良行为信息列表
				IGMCompanyPoorInfo igmcpi = new IGMCompanyPoorInfo();
				igmcpi.setCompanyGuid(companyGuid);
				igmcpi.setQualificationType(ccr.getQualificationType());
				List<IGMCompanyPoorInfo> igmcpiList = enterpriseMapper.selectIGMCompanyPoorInfoListByCompanyGuid(igmcpi);
				List<BasicScoreContent> companyPoorInfoList = new ArrayList<BasicScoreContent>();
				if(!StringUtils.isEmpty(igmcpiList)) {
					for(IGMCompanyPoorInfo cpi:igmcpiList) {
						BasicScoreContent companyPoorInfo = new BasicScoreContent();
						companyPoorInfo.setDate(cpi.getPunishmentDate());
						companyPoorInfo.setContent(cpi.getBehaviorDescription());
						companyPoorInfo.setScore(Double.valueOf(cpi.getPenaltyScores()));
						companyPoorInfoList.add(companyPoorInfo);
					}
				}
				if(!StringUtils.isEmpty(ccr.getProfessional()) && !StringUtils.isEmpty(ccr.getQualificationSequence())) {
					ccr.setTableName(ccr.getProfessional()+ccr.getQualificationSequence()+"资质");
				}else if(StringUtils.isEmpty(ccr.getProfessional()) && !StringUtils.isEmpty(ccr.getQualificationSequence())) {
					ccr.setTableName(ccr.getQualificationSequence()+"资质");
				}else if(!StringUtils.isEmpty(ccr.getProfessional()) && StringUtils.isEmpty(ccr.getQualificationSequence())) {
					ccr.setTableName(ccr.getProfessional()+"资质");
				}else if(StringUtils.isEmpty(ccr.getProfessional()) && StringUtils.isEmpty(ccr.getQualificationSequence())) {
					ccr.setTableName(ccr.getQualificationType()+"资质");
				}
				IGMCompanyStageRank igmcsr = new IGMCompanyStageRank();
				igmcsr.setCompanyGuid(companyGuid);
				igmcsr.setQualificationType(ccr.getQualificationType());
				igmcsr.setQualificationSequence(ccr.getQualificationSequence());
				igmcsr.setProfessional(ccr.getProfessional());
				IGMCompanyStageRank csr = enterpriseMapper.selectIGMCompanyStageRank(igmcsr);
				ccr.setCompanyStageRank(csr);
				List<BasicScoreContent> basicScoreContentList = new ArrayList<BasicScoreContent>();
				if(!StringUtils.isEmpty(ccr.getBasicScoreContent()) && StrUtil.containsAny(ccr.getBasicScoreContent(), "|")) {
					if(StrUtil.containsAny(ccr.getBasicScoreContent(), "@")) {
						String[] subStr=StrUtil.split(ccr.getBasicScoreContent(), "@");
						BasicScoreContent bsc = new BasicScoreContent();
						if(!StringUtils.isEmpty(subStr)) {
							for(String ss:subStr) {
								String[] subMyStr=StrUtil.split(ss, "|");
								if(subMyStr.length>=2) {
									bsc.setContent(subMyStr[0]);
									bsc.setScore(Double.valueOf(subMyStr[1]));
								}
								basicScoreContentList.add(bsc);
							}
						}
					}else {
						String[] subStr=StrUtil.split(ccr.getBasicScoreContent(), "|");
						BasicScoreContent bsc = new BasicScoreContent();
						if(subStr.length>=2) {
							bsc.setContent(subStr[0]);
							bsc.setScore(Double.valueOf(subStr[1]));
						}
						basicScoreContentList.add(bsc);
					}
				}
				ccr.setBasicScoreContentList(basicScoreContentList);
				ccr.setCompanyWellInfoList(companyWellInfoList);
				ccr.setCompanyPoorInfoList(companyPoorInfoList);
				//根据不同类型的企业处理不同的分数
				if(ccr.getQualificationType().equals("施工企业")) {
					ccr.setBadBeginScore("150");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}else if(ccr.getQualificationType().equals("检测机构")) {
					ccr.setBadBeginScore("60");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}else if(ccr.getQualificationType().equals("混凝土生产企业")) {
					ccr.setBadBeginScore("60");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}else if(ccr.getQualificationType().equals("工程招标代理机构")) {
					ccr.setBadBeginScore("100");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}else if(ccr.getQualificationType().equals("建设单位")) {
					ccr.setBadBeginScore("120");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}else if(ccr.getQualificationType().equals("工程造价咨询企业")) {
					ccr.setBasicAddScore(new BigDecimal(ccr.getBasicScore()).divide(new BigDecimal("0.2")).toString());
					ccr.setBadBeginScore("100");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore()).divide(new BigDecimal("0.3"))).toString());
					ccr.setGoodAddScore(new BigDecimal(ccr.getGoodScore()).divide(new BigDecimal("0.1")).toString());
					List<BasicScoreContent> superviseInfoList =new ArrayList<BasicScoreContent>();
					//质量监督行为中的成果文件备案个数得分
					BasicScoreContent document = new BasicScoreContent();
					document.setDate(DateUtil.format(DateUtil.offset(DateUtils.parseDate(StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7)), DateField.YEAR, -1), "yyyy年MM月")+"至"+StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7));
					document.setContent(documentScoreContent);
					document.setScore(Double.parseDouble(new BigDecimal(ccr.getDocumentScore()).divide(new BigDecimal("0.4")).toString()));
					superviseInfoList.add(document);
					//质量监督行为中的二维码载明工程标的额排名得分
					BasicScoreContent twoCode = new BasicScoreContent();
					twoCode.setDate(DateUtil.format(DateUtil.offset(DateUtils.parseDate(StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7)), DateField.YEAR, -1), "yyyy年MM月")+"至"+StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7));
					twoCode.setContent(twoCodeScoreContent);
					twoCode.setScore(Double.parseDouble(new BigDecimal(ccr.getTwoCodeScore()).divide(new BigDecimal("0.4")).toString()));
					superviseInfoList.add(twoCode);
					//质量监督行为中的工程造价咨询收入得分
					BasicScoreContent price = new BasicScoreContent();
					price.setDate(DateUtil.format(DateUtil.offset(DateUtils.parseDate(StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7)), DateField.YEAR, -1), "yyyy年MM月")+"至"+StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7));
					price.setContent(priceScoreContent);
					price.setScore(Double.parseDouble(new BigDecimal(ccr.getPriceScore()).divide(new BigDecimal("0.4")).toString()));
					superviseInfoList.add(price);
					//质量监督行为中的企业未被投诉得分
					BasicScoreContent noComplaint = new BasicScoreContent();
					noComplaint.setDate(DateUtil.format(DateUtil.offset(DateUtils.parseDate(StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7)), DateField.YEAR, -1), "yyyy年MM月")+"至"+StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7));
					noComplaint.setContent(noComplaintScoreContent);
					noComplaint.setScore(Double.parseDouble(new BigDecimal(ccr.getNoComplaintScore()).divide(new BigDecimal("0.4")).toString()));
					superviseInfoList.add(noComplaint);
					//质量监督行为中的成果文件专项质量检查得分
					BasicScoreContent inspection = new BasicScoreContent();
					inspection.setDate(DateUtil.format(DateUtil.offset(DateUtils.parseDate(StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7)), DateField.YEAR, -1), "yyyy年MM月")+"至"+StrUtil.subWithLength(StrUtil.trim(ccr.getPubDate()),0,7));
					inspection.setContent(inspectionScoreContent);
					inspection.setScore(Double.parseDouble(new BigDecimal(ccr.getInspectionScore()).divide(new BigDecimal("0.4")).toString()));
					superviseInfoList.add(inspection);
					ccr.setSuperviseInfoList(superviseInfoList);
					//计算质量监督行为的分数
					ccr.setSuperviseScore(new BigDecimal(ccr.getDocumentScore()).add(new BigDecimal(ccr.getTwoCodeScore())).add(new BigDecimal(ccr.getPriceScore())).add(new BigDecimal(ccr.getNoComplaintScore())).add(new BigDecimal(ccr.getInspectionScore())).toString());
					ccr.setSuperviseAddScore(new BigDecimal(ccr.getSuperviseScore()).divide(new BigDecimal("0.4")).toString());
				}else {
					ccr.setBadBeginScore("120");
					ccr.setBadSubScore(new BigDecimal(ccr.getBadBeginScore()).subtract(new BigDecimal(ccr.getBadScore())).toString());
				}
				IGMCompanyStageRank clsr = enterpriseMapper.selectIGMCompanyLastStageRank(igmcsr);
				ccr.setCompanyLastStageRank(clsr);
			}
		}
		return igmccrList;
	}

	@Override
	public BaseBulidUnitInfo selectBaseBulidUnitInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return enterpriseMapper.selectBaseBulidUnitInfoByGuid(guid);
	}
   
}
